﻿using System;
using System.Collections.Generic;
using Looptribe.Moodlin8.Model;
using Looptribe.Moodlin8.ViewModel;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The Grouped TopItems Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234231

namespace Looptribe.Moodlin8.View
{
    /// <summary>
    /// A page that displays a grouped collection of items.
    /// </summary>
    public sealed partial class AssignmentPage : Looptribe.Moodlin8.Common.LayoutAwarePage
    {
        public AssignmentPage()
        {
            this.InitializeComponent();
        }

        public AssignmentViewModel ViewModel { get { return (AssignmentViewModel)DataContext; } }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            var assignment = navigationParameter as AssignmentWrapper;
            ViewModel.LoadContent(assignment);

            var dataTransferManager = DataTransferManager.GetForCurrentView();
            dataTransferManager.DataRequested += DataTransferManagerDataRequested;
        }

        private void DataTransferManagerDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var request = args.Request;
            request.Data.Properties.Title = ViewModel.Assignment.Title;
            request.Data.SetHtmlFormat(HtmlFormatHelper.CreateHtmlFormat(ViewModel.Assignment.Intro));
        }
    }
}
